<template>
  <div>
    <PageTemplate>
      <div slot="content">
        <PageEditCollapse :is-search="false" :default-collapse="true">
          <div slot="title" class="page-edit-colllapse-title">
            <div style="width: 200px;">{{$t('cm.baseinfo')}}</div>
          </div>
          <div>
            <!-- 表单 -->
            <el-form
              :model="standard"
              label-position="right"
              label-width="150px"
              class="page-edit-colllapse-form"
              :rules="rules"
              ref="standard"
            >
              <el-row>
                <el-col :span="12">
                  <el-form-item :label="$t('sri.subject_Code')" prop="subjectCode">
                    <el-input v-model="standard.subjectCode"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item :label="$t('sri.subject_Name')" prop="subjectName">
                    <el-input v-model="standard.subjectName"></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="12">
                  <el-form-item :label="$t('sri.subject_year')" prop="year">
                    <el-input v-model="standard.year"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item :label="$t('sri.subject_level')" prop="subjectType">
                    <el-select v-model="standard.subjectType" filterable placeholder="请选择">
                      <el-option
                        v-for="(item, index) in levels"
                        :key="index"
                        :label="item.dictText"
                        :value="item.dictKey"
                      ></el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="12">
                  <el-form-item :label="$t('sri.standard_editor')" prop="editor">
                    <el-select v-model="standard.editor" filterable placeholder="请选择">
                      <el-option
                        v-for="(item, index) in editors"
                        :key="index"
                        :label="item.dictText"
                        :value="item.dictKey"
                      ></el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item :label="$t('sri.subject_deptName')" prop="deptName">
                    <el-select v-model="standard.deptName" filterable placeholder="请选择">
                      <el-option
                        v-for="(item, index) in subjectDepts"
                        :key="index"
                        :label="item.dictText"
                        :value="item.dictKey"
                      ></el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="12">
                  <el-form-item label="日期" prop="tempTimes">
                    <el-date-picker
                      style="width: 80%"
                      v-model="standard.tempTimes"
                      type="daterange"
                      range-separator="至"
                      start-placeholder="开始日期"
                      end-placeholder="结束日期">
                    </el-date-picker>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item :label="$t('sri.standard_finishName')" prop="finishName">
                    <el-select v-model="standard.finishName" filterable placeholder="请选择">
                      <el-option
                        v-for="(item, index) in finishNames"
                        :key="index"
                        :label="item.dictText"
                        :value="item.dictKey"
                      ></el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
                <!-- <el-col :span="12">
                  <el-form-item :label="$t('sri.subject_startTime')" prop="startTime">
                    <el-date-picker v-model="standard.startTime" type="date" placeholder="选择日期"></el-date-picker>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item :label="$t('sri.subject_endTime')" prop="endTime">
                    <el-date-picker v-model="standard.endTime" type="date" placeholder="选择日期"></el-date-picker>
                  </el-form-item>
                </el-col> -->
              </el-row>
              <el-row>
                <el-col :span="12">
                  <el-form-item :label="$t('sri.subject_User')" prop="subjectUser">
                    <PersonSelect id="subjectUser" :value="standard.subjectUser" v-bind:multiple="true"  ></PersonSelect>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item :label="$t('sri.standard_publish')" prop="publish">
                    <el-radio v-model="standard.publish" label="Y">是</el-radio>
                    <el-radio v-model="standard.publish" label="N">否</el-radio>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
              </el-row>
            </el-form>
          </div>
        </PageEditCollapse>
        <PageEditCollapse :is-search="false" :default-collapse="true">
          <div slot="title" class="page-edit-colllapse-title">
            <div style="width: 200px;">成果文件</div>
          </div>
          <div>
            <FileUploadCommon 
              @uploadSuccess="uploadSuccess" 
              @uploadRemove="uploadRemove" 
              :fileList="fileList"
              >
              </FileUploadCommon>
          </div>
        </PageEditCollapse>
        <el-row class="row-empty-10"></el-row>
        <el-row>
          <el-button type="primary" @click="submitForm('standard')">{{$t('cm.commit')}}</el-button>
          <el-button type="primary" @click="toList">{{$t('cm.return')}}</el-button>
        </el-row>
      </div>
    </PageTemplate>
  </div>
</template>

<script>
import { ALERT } from '@/utils/responseHandle'
import * as Constant from '@/utils/constant'
import PageTemplate from '@/components/common/PageTemplate'
import PageEditCollapse from '@/components/common/PageEditCollapse'
//import FileUploadMeeting from '@/components/common/FileUploadMeeting'
import FileUploadCommon from '@/components/common/FileUploadCommon'
//import HasBtn from '@/mixins/HasBtn'
//import { mapState } from 'vuex'
import { COMMON } from '@/utils/commonUtil'
import api_sri from '@/api/api_sri'
import sys_manage from '@/api/sys_manage'
import PersonSelect from '@/components/asc/PersonSelect'

const vailSubjectUserName=(rule,value,callback)=>{
  if(document.getElementById("subjectUser").value) {
    callback();
  }else{
    callback(new Error("必填项"));
  }
};

export default {
  components: {
    PageTemplate, PageEditCollapse, PersonSelect,FileUploadCommon
  },
  data () {
    return {
      standard: {
        id: null,
        subjectCode: null,
        subjectName: null,
        year: null,
        subjectType: null,
        subjectUser: null,
        editor: null,
        deptId: 11,
        deptName: null,
        startTime: null,
        endTime: null,
        finishId: 1,
        finishName: null,
        publish: null,
        tempTimes:null
      },
      subjectDepts: [],
      levels:[],
      finishNames:[],
      standardFiles:[],
      editors:[],
      rules: {
        subjectCode: [
          { required: true, message: '必填项', trigger: 'blur' }
        ],
        subjectName: [
          { required: true, message: '必填项', trigger: 'blur' }
        ],
        year: [
          { required: true, message: '必填项,且为整数',  trigger: 'blur' }
        ],
        subjectUser: [
          { required: true, validator: vailSubjectUserName, trigger: 'blur' }
        ],
        deptName: [
          { required: true, message: '必填项', trigger: 'blur' }
        ],
        tempTimes: [
          { required: true, message: '必选项', trigger: 'blur' }
        ],
        // startTime: [
        //   { required: true, message: '必填项', trigger: 'blur' }
        // ],
        // endTime: [
        //   { required: true, message: '必填项', trigger: 'blur' }
        // ],
        finishName: [
          { required: true, message: '必填项', trigger: 'blur' }
        ],
        publish: [
          { required: true, message: '必填项', trigger: 'blur' }
        ],
        editor: [
          { required: true, message: '必填项', trigger: 'blur' }
        ]
      },
      fileList:[]
    }
  },
  created () {
    this.sysDictLoadByPaging();
    if (this.$router.history.current.params.id !== Constant.NEW_PARAMS) {
      this.sriGainStandardGet(this.$router.history.current.params.id)
    }
  },
  methods: {
    sriGainStandardGet (id) {
      api_sri.sriGainStandardGet({ id }, result => {
        if (result.code === Constant.RT_SUCCESS) {
          this.standard = {
            ...result.data,
            tempTimes: [result.data.startTime, result.data.endTime]
          }
        } else {
          // _this.$t('cm.tips')
          ALERT.error(result.msg)
        }
      })
    },
    sysDictLoadByPaging () {
      sys_manage.sysDictLoadByPaging({dictGroupKey: 'achievement_profession_dept'}, result => {
        if (result.code === Constant.RT_SUCCESS) {
          this.subjectDepts = result.data
        } else {
          ALERT.error(result.msg)
        }
      })
      sys_manage.sysDictLoadByPaging({dictGroupKey: 'standard_level'}, result => {
        if (result.code === Constant.RT_SUCCESS) {
          this.levels = result.data
        } else {
          ALERT.error(result.msg)
        }
      })
      sys_manage.sysDictLoadByPaging({dictGroupKey: 'standard_responsible_unit'}, result => {
        if (result.code === Constant.RT_SUCCESS) {
          this.finishNames = result.data
        } else {
          ALERT.error(result.msg)
        }
      })
      sys_manage.sysDictLoadByPaging({dictGroupKey: 'chief_less_editor'}, result => {
        if (result.code === Constant.RT_SUCCESS) {
          this.editors = result.data
        } else {
          ALERT.error(result.msg)
        }
      })
    },
    submitForm (formName) {
      this.$refs[formName].validate((valid, error) => {
        if (valid) {
          this.standard.subjectUser = document.getElementById("subjectUser").value;
          if (this.standard.tempTimes && this.standard.tempTimes.length === 2) {
            this.standard.startTime = this.standard.tempTimes[0]
            this.standard.endTime = this.standard.tempTimes[1]
          } 
          let d = { standard: this.standard }
          let standardFiles = this.fileList.map(item => {
            return {
              standardId: this.standard.id,
              fileId: item.fileId,
              fileName: item.name,
              filePath: item.url
            }
          })
          d.standardFiles = standardFiles
          api_sri.sriGainStandardSave(d, result => {
            if (result.code === Constant.RT_SUCCESS) {
              ALERT.success(result.msg)
              this.toList()
            } else {
              // _this.$t('cm.tips')
              ALERT.error(result.msg)
            }
          })
        } else {
          return false
        }
      })
    },
    toList () {
      COMMON.routerTo(Constant.ROUTE_SRI_GAIN_STANDARD.index, this.$router)
    },
    uploadSuccess (res, fileKey) {
      if (res.data && res.data[0]) {
        let file = {name: res.data[0].fileName, url: res.data[0].url, fileId: res.data[0].fileId}
        this.fileList.push(file)
      }
    },
    uploadRemove (fileList, fileKey) {
      let resList = fileList.map(item => {
        return {
          name: item.name,
          url: item.url,
          fileId: item.fileId
        }
      })
      this.fileList = resList
    }
  },
  mounted () {
  },
  computed: {
  }
}
</script>

<style lang="scss">
</style>
